<?php
$oDataIni = $oDataAge;
$ymdIni = $oDataIni->getYMD();
$aWhere = array(" a.tipo_agenda <> 'C' ");
$aWhere[] = " a.data_agenda >= '".$oDataIni->getYMD()."' ";
if ($sListaTI != 'all') {
	$aWhere[] = " (a.tipo_agenda = 'M' OR a.id_tipo_imp IN ($sListaTI)) ";
}

if ($sViewMemo == 'N') {
	$aWhere[] = " a.tipo_agenda = 'I' ";
}

$sWhere = (empty($aWhere))?"":" WHERE ".implode (" AND ",$aWhere);
				
$sSql = "SELECT DISTINCT a.id_agenda, a.giorno, a.mese, a.anno, a.ora, a.minuti ".
		", a.data_agenda, a.data_memo_iniz, a.tipo_agenda \n" .
		" FROM ovf_agenda a \n" .
		" INNER JOIN ovf_agenda_utenti au ON au.id_agenda = a.id_agenda \n".
		"    AND au.id_utente IN ($sListaAgende) \n".
		" $sWhere \n".
		" ORDER BY a.tipo_agenda DESC, a.data_agenda \n";
//dbg_($sSql);
$objDB->query($sSql,'selId');
$aId = array();
while($acX = $objDB->fetch('selId')) {
	if ($acX['giorno'] == 0) {
		$ymdX = '0000-00-00';
	} elseif ($acX['mese'] == 0) {
		$ymdX = date('Y-m-d',mktime(0,0,0,$oDataIni->month(),$acX['giorno'],$oDataIni->year()));
		if ($ymdX < $oDataIni->getYMD()) {
			$ymdX = date('Y-m-d',mktime(0,0,0,$oDataIni->month()+1,$acX['giorno'],$oDataIni->year()));
		}
	} elseif ($acX['anno'] == 0) {
		$ymdX = date('Y-m-d',mktime(0,0,0,$acX['mese'],$acX['giorno'],$oDataIni->year()));
		if ($ymdX < $oDataIni->getYMD()) {
			$ymdX = date('Y-m-d',mktime(0,0,0,$acX['mese'],$acX['giorno'],$oDataIni->year()+1));
		}
	} else {
		$ymdX = $acX['data_agenda'];
	}
	if ($ymdX == $oDataIni->getYMD() || count($aId) < 50) {
		$aId[] = "A".$ymdX.lpad($acX['ora'],2,'0').lpad($acX['minuti'],2,'0').$acX['id_agenda'];
	}
	if ($acX['anno'] > 0 &&  $ymdX > $oDataIni->getYMD() && count($aId) > 50) {
		break;
	}
}
sort($aId);
$aId = array_slice($aId, 0,50);
//dbg_($aId);
$sCmdL  = "Da ".str_replace(" ","&nbsp;",$oDataIni->getDateText());
	;
$sCmdR  = "";

$sCmdR  .= "&nbsp;&nbsp;&nbsp";
$sCmdR  .= "<a href='agenView.php?dataAge=".$oDataIni->getDayAdd(-1)."&$sXSNQS' class='cmd1'>"
	._t("giorno prec.")."</a>\n";
$sCmdR  .= "&nbsp;&nbsp;&nbsp";
$sCmdR  .= "<a href='agenView.php?dataAge=".$oDataIni->getDayAdd(1)."&$sXSNQS' class='cmd1'>"
	._t("giorno succ.")."</a>\n";
$sCmdR .=  cmdTorna();
testaHtml('Agenda', 'Lista impegni/memo',$msgPag, 'agen');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<form name='frm1' action='<?=$_SERVER['PHP_SELF']?>' method='post' enctype="multipart/form-data">
<input type='hidden' name='azione' value="...">
<input type='hidden' name='xsn' value="<?=XSN?>">
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?php echo getHtmlRigaRif(20);?>
	<tr>
		<td class="lab1" colspan='3' align='center'>
			<?=_t("Data")?>
		</td>
		<td class="lab1" colspan='2' align='center'>
			<?=_t("Ora")?>
		</td>
		<td class="lab1" colspan='12' align='center'>
			<?=_t("Oggetto")?>
		</td>
		<td class="lab1" colspan='3' align='center'>
			<?=_t("Dove")?>
		</td>
	</tr>
<?php 
$conta=0;
$ymdOld = '';
$sCla = 'txt1';
$oDataX = new ovfDate();
foreach($aId as $kX => $sX) {
	$sSql = "SELECT a.*, CONCAT(u.cognome) AS nominativo "
		.", ti.descr AS TI_des, ti.pallino AS TI_pallino, an.intestaz1 "
		." FROM ovf_agenda a "
		." LEFT JOIN ovf_agenda_utenti au ON au.id_agenda = a.id_agenda "
		." LEFT JOIN ovf_utenti u ON u.id_utente = au.id_utente "
		." LEFT JOIN ovf_agenda_tipi_imp ti ON ti.id_tipo_imp = a.id_tipo_imp \n"
		." LEFT JOIN ovf_anagrafiche an ON an.id_anag = a.id_anag \n"
      ." WHERE a.id_agenda = ".substr($sX,15);
	$objDB->query($sSql,'selAge');	
	$acX = $acAge = $objDB->fetch('selAge');
	$sAge = '(';
	while($acX) {
		$sAge .= $acX['nominativo'].",";
		$acX = $objDB->fetch('selAge');
	}
	$sAge = substr($sAge,0,-1).')';
	if ($acAge['tipo_agenda'] == 'M') {
		$sHref = "memoView.php";
	} else {
		$sHref = "impeView.php";
	}
	$sHref .= "?id=".$acAge['id_agenda']."&$sXSNQS";
	if ($acAge['tipo_agenda'] == 'M' && $acAge['giorno'] == 0){
		$ymdX = '';
	} else {
		$ymdX = substr($sX,1,10);
	}
	if ($ymdX == '' || $ymdX==$ymdOld) {
		$sDataTxt = "";
	} else {
		$sCla = ($sCla=='txt1')?'txt2':'txt1';
		if ($oDataX->isHoliday('',$ymdX,'ymd')) {
			$sStyle = "color:red;";
		} else {
			$sStyle = "";
		}
		$sDataTxt = "<a href='agenView.php?vistaAge=List&dataAge=".urlencode($oDataX->getDMY('/',4,$ymdX,'ymd'))
			."&$sXSNQS' style='$sStyle' class='$sCla'>";
		$sDataTxt .= $oDataX->getDateText('text-short',$ymdX,'ymd')."</a>";
	}
	$sStyle = ($ymdX == '' || $ymdIni == $ymdX)?"font-weight:bold;":"";
	$ymdOld = $ymdX;
	?>
	<tr>
		<td class="<?=$sCla?>" colspan='3' style='text-align:right;<?=$sStyle?>'>
			<?php 
			echo $sDataTxt;
			?>
		</td>
		<td class="<?=$sCla?>" colspan='2' style='text-align:left;<?=$sStyle?>'>
		<?php 			
			if ($acAge['tipo_agenda'] == 'M') {
				echo lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."&nbsp;";
				if ($acAge['giorno']==0) {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_g.gif' border='0'/>";
				} elseif ($acAge['mese']==0) {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_m.gif' border='0'/>";
				} elseif ($acAge['anno']==0) {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_a.gif' border='0'/>";
				} else {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_x.gif' border='0'/>";
				}
			} else {
				if ($acAge['durata_minuti'] >= 1440) {
					echo _t("giornata");
				} else {
					echo lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."&nbsp;-&nbsp;";
					$nT = mktime($acAge['ora'],$acAge['minuti'],0,01,01,2001)+$acAge['durata_minuti']*60;
					$sOraFine = date('H:i',$nT);
					if ($sOraFine > "24:00") $sOraFine = "24:00";
					echo $sOraFine;
				}
				
			}
			?>
		</td>
		<td class="<?=$sCla?>" colspan='12' style='padding-left:20px;text-indent:-15px;'>
			<?
         if ($acAge['tipo_agenda'] == 'M') {
            $pallinoImg = "trasp.gif" ;
         } elseif ($acAge['TI_pallino']=='') {
            $pallinoImg = "vuoto.png";
         } else {
            $pallinoImg = $acAge['TI_pallino'].".gif" ;
         }
			echo "<img src=\"".OVF_URL_ROOT."/app/_img/pallino_ti_$pallinoImg\" border='0'/>&nbsp;&nbsp;";
         //echo $sAge.' ';
			echo "<a href='$sHref' class='$sCla'>";
			echo $acAge['oggetto'];
			echo "</a>\n";
         if (!empty($acAge['intestaz1'])) {
            echo " - <a href='../rcom/anag/anagView.php?idAnag=".$acAge['id_anag']."&xsn=".XSN."' class='$sCla'>".$acAge['intestaz1']."</a>\n";
         }
         ?>
		</td>
		<td class="<?=$sCla?>" colspan='3' align='left'>
			<?php 
			echo $acAge['dove'];
			?>
		</td>
	</tr>
<?php 
}
?>
</table>
</form>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<?
codaHtml();
exit();

